package com.ciban.smarthome.module.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.engine.LampEngine;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.entity.Equip_Lamp;
import com.ciban.smarthome.module.net.communication.Communicate.ConnListener;
import com.ciban.smarthome.module.net.protocol.Protocol;
import com.ciban.smarthome.module.ui.adapter.LightsLvAdapter;
import com.ciban.smarthome.module.ui.adapter.LightsLvAdapter.LightAdapterListener;

/**
 * 跳转需要带参数
 *
 * @author lenovo
 */
public class E_LampDetailsActivity extends BaseActivity implements OnClickListener {
    private static final int INTENT_ADD_LIGHT = 100;
    private static final int HANDLER_REFRESH_LAMP_STATUS = 101;

    private ListView lv_lists;
    private ImageView iv_add;

    private String area_id;
    private int equip_type;

    private ArrayList<Equip_Lamp> lightList;

    private LightsLvAdapter adapter;


    private LampEngine engine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_equip_lamp_details);

        initData();

        initViews();
    }

    private void initData() {
        area_id = getIntent().getStringExtra("area_id");
        equip_type = getIntent().getIntExtra("type", 0);

        engine = new LampEngine(this, equip_type, area_id);
        lightList = engine.getLightList();
    }

    /**
     * 刷新电灯的当前状态
     */
    private void refreshHostStatus() {
        if (lightList.size() < 1)
            return;

        engine.sendCmd(Protocol.CMD_QUERY_STATUS, new ConnListener() {

            @Override
            public void onSuccessed(String result) {
                engine.parseLampStatus(result);
                handler.sendEmptyMessage(HANDLER_REFRESH_LAMP_STATUS);
            }

            @Override
            public void onFailed(String err) {

            }
        });
    }

    private void initViews() {
        ((TextView) findViewById(R.id.title)).setText("灯控制");
        ((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);
        iv_add = ((ImageView) findViewById(R.id.title_add));
        lv_lists = (ListView) findViewById(R.id.light_lists);

        adapter = new LightsLvAdapter(this, lightList);
        lv_lists.setAdapter(adapter);

        setListeners();

        refreshHostStatus();
    }

    private void setListeners() {
        iv_add.setOnClickListener(this);
        adapter.setListener(lampAdapterListener);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.title_add:
                Intent intent = new Intent(E_LampDetailsActivity.this,
                        E_LampModifyActivity.class);
                intent.putExtra("type",equip_type);
                startActivityForResult(intent, INTENT_ADD_LIGHT);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case INTENT_ADD_LIGHT:
                if (resultCode == RESULT_OK) {
                    engine.refreshLightList();
                    adapter.notifyDataSetChanged();
                }
                break;
        }
    }

    @Override
    protected void handMsg(Message msg) {
        super.handMsg(msg);
        switch (msg.what) {
            case HANDLER_REFRESH_LAMP_STATUS:
                adapter.notifyDataSetChanged();
                break;
        }
    }

    private LightAdapterListener lampAdapterListener = new LightAdapterListener() {

        @Override
        public void LightStatusChanged(final int position, boolean checked) {
            Equip_Lamp info = lightList.get(position);
            if (checked) {
                engine.sendCmd(info, Protocol.COMMON_OPEN, new ConnListener() {
                    @Override
                    public void onSuccessed(String result) {
                        LogUtil.d(E_LampDetailsActivity.class,"打开电灯成功");
                        lightList.get(position).setStatus(1);
                    }

                    @Override
                    public void onFailed(String err) {
                        LogUtil.d(E_LampDetailsActivity.class,"打开电灯失败："+err);
                    }
                });

            } else {
                engine.sendCmd(info, Protocol.COMMON_CLOSE, new ConnListener() {
                    @Override
                    public void onSuccessed(String result) {
                        lightList.get(position).setStatus(0);
                    }

                    @Override
                    public void onFailed(String err) {

                    }
                });

            }
        }

        @Override
        public void LightLevelChanged(int position, int progress) {

        }

        @Override
        public void clickItem(int position) {
            Equip_Lamp info = lightList.get(position);
            Intent intent = new Intent(E_LampDetailsActivity.this,
                    E_LampModifyActivity.class);
            intent.putExtra("light", info);
            LogUtil.info("选中电灯的信息："+info.toString());
            startActivityForResult(intent, INTENT_ADD_LIGHT);
        }

        @Override
        public boolean longClickItem(int position) {
            final Equip_Lamp info = lightList.get(position);
            showCommonAlertDialog("提示",
                    "是否删除  " + info.getName() + "?",
                    new alertDialogListener() {

                        @Override
                        public void alertDialogPositive() {
                            if(engine.delLamp(info)){
                                adapter.notifyDataSetChanged();
                            }
                        }
                        @Override
                        public void alertDialogNegative() {

                        }
                    });
            return true;
        }
    };

}
